<?php

namespace app\common\dao\repair;

use think\db\BaseQuery;
use app\common\dao\BaseDao;
use app\traits\CategoryDao;
use app\common\model\repair\RepairCateModel;

class RepairCateDao extends BaseDao
{

    use CategoryDao;

    /**
     * @return RepairCateModel
     */
    protected function getModel(): string
    {
        return RepairCateModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {

        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
                $query->where('pid', (int)$where['pid']);
            })
            ->when(isset($where['is_show']) && $where['is_show'] !== '', function ($query) use ($where) {
                $query->where('is_show', (int)$where['is_show']);
            })
            ->when(isset($where['level']) && $where['level'] !== '', function ($query) use ($where) {
                $query->where('level', '<=', (int)$where['level']);
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('cate_name', '%' . trim($where['keywords']) . '%');
            });
    }

    /**
     * 根据条件查全部
     * @param $propertyId
     * @param $isShow
     * @param $level
     * @param $field
     * @return array|\think\Collection|BaseQuery[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-12 11:13
     */
    public function getAll($propertyId = null, $isShow = '', $level = '', $field = '*')
    {
        $query = $this->search([
            'is_show' => $isShow,
            'level' => $level
        ], $propertyId);
        return $query->field($field)->order('sort DESC,' . $this->getPk() . ' DESC')->select();
    }


}
